home *** CD-ROM | disk | FTP | other *** search
/ EDUCORP 8 / Educorp2Compilation.sit / educorp2 / Utilities (2100) / 2108 Utilities v.5 / Display / Display.c next >
Encoding:
C/C++ Source or Header  |  1985-11-29  |  9.8 KB  |  367 lines

  1. #ifdef NEVER
  2. This is a little program I whipped up in Megamax C that will display any
  3. Mac file of type TEXT.  It is very handy for looking at text files you have
  4. downloaded from a BBS without having to load a text editor.  The program
  5. loads in about 4 seconds and supports standard Mac desk accessorys...etc.
  6.  
  7. Let me know if you have any problems with the program
  8. #endif
  9.  
  10. /* This sample program allows a user to open a file and print it's contents
  11. ** to a window on the Mac screen.  To give the compiled program an Icon
  12. ** copy the resources from Display.rsrc (use Rmover or Resource Editor) and
  13. ** paste them into the compiled file.  Then use SetFile to change the creator
  14. ** name of the compiled file to DFIL and set the bundle bit.  When you 
  15. ** return to the finder the program icon should have an icon.
  16. ** By Greg Corson
  17. ** 19141 Summers Drive
  18. ** South Bend, IN 46637
  19. ** (219) 272-2136
  20. ** UUCP: {ihnp4 | ucbvax}!pur-ee!kangaro!milo
  21. ** ARPA: pur-ee!kangaro!milo@Purdue.ARPA
  22. ** EDU: kangaro!milo@ee.Purdue.EDU
  23. ** Or call my BBS at (219) 277-5825
  24. */
  25.  
  26. #include <qd.h>  
  27. #include <win.h>
  28. #include <menu.h>
  29. #include <event.h>
  30. #include <pack.h>
  31. #include <font.h>
  32.  
  33. #define LASTMENU 4
  34. #define APPLEMENU 1
  35. #define FILEMENU 256
  36. #define EDITMENU 257
  37. #define STOPMENU 258
  38. #define NULL 0L
  39. #define FALSE 0
  40. #define TRUE 1
  41. #define eoferr (-39)
  42.  
  43. rgnhandle    updateregn;
  44. menuhandle   mymenus[LASTMENU+1];
  45. rect         screenrect, dragrect, prect;
  46. boolean      doneflag, temp;
  47. eventrecord  myevent;
  48. int          code, refnum;
  49. windowrecord wrecord;
  50. windowptr    mywindow, whichwindow;
  51. grafptr      temport;
  52. int          themenu, theitem;
  53. int          fileopen,wide,fd1;
  54. long         count;
  55. char         tempbuf[32];
  56.  
  57. main()
  58. {
  59.    #include <qdvars.h>    /* quickdraw globals */
  60.    int i,j;
  61.  
  62. /* Initialize variables */
  63.  
  64.    j = 0;
  65.    doneflag = FALSE;
  66.    fileopen = FALSE;
  67.    
  68. /* Initialize quickdraw, fonts, events, windows, menus, dialogs and cursor */
  69.    
  70.    initgraf(&theport);
  71.    initfonts();
  72.    flushevents(everyevent, 0);
  73.    initwindows();
  74.    initmenus();
  75.    teinit();
  76.    initdialogs(NULL);
  77.    initcursor();
  78.  
  79. /* Create an empty region pointer for use by scrollrect later */
  80.  
  81.    updateregn=newrgn();
  82.  
  83. /* Setup the menu bar */
  84.  
  85.    setupmenus();
  86.    
  87. /* Setup the drag rectangle so part of the window will always be visible */
  88.  
  89.    setrect(&screenrect, 4, 40, 508, 338);
  90.    setrect(&dragrect, 4, 24, screenrect.a.right-4, screenrect.a.bottom-4);
  91.    
  92. /* Create the window and set the current port to the window port */
  93.    
  94.    mywindow = newwindow(&wrecord, &screenrect, "Display a file", TRUE, 0,
  95.              (long)-1, FALSE, (long)0);
  96.    setport(mywindow);
  97.  
  98. /* get the rectangle for the current window and put it in prect */
  99.  
  100.    blockmove(&theport->portrect, &prect, (long)sizeof prect);
  101.    wide = prect.a.right  - prect.a.left;
  102.    
  103. /* Now that the window and menus are drawn set the window font to monaco 9 */
  104.    
  105.    textfont(monaco);
  106.    textsize(9);
  107.    moveto(prect.a.left+1,prect.a.bottom-2);
  108.  
  109. /* Main loop to process events */
  110.    
  111.    do {
  112.  
  113. /**** If a file is open copy a line to the output window */
  114.  
  115.       if(fileopen)
  116.          {
  117.      count=32;
  118.      fsread(fd1, &count, tempbuf);
  119.      if(count == 0)
  120.         {
  121.         fsclose(fd1);
  122.         fileopen=FALSE;
  123.         moveto(prect.a.left+1,prect.a.bottom-2);
  124.         scrollrect(&prect,0,-11,updateregn);
  125.         drawstring("-------End of File-------");
  126.         scrollrect(&prect,0,-11,updateregn);
  127.         moveto(prect.a.left+1,prect.a.bottom-2);
  128.         }
  129.      else
  130.         {
  131.         for(i = 0; i < count; i++)
  132.            {
  133.            if(tempbuf[i] > 31)
  134.               drawchar(tempbuf[i]);
  135.            else
  136.               {
  137. /**************** Scroll window if we get a carriage return */
  138.                   if(tempbuf[i] == '\r')
  139.              {
  140.              j = 0;
  141.              scrollrect(&prect,0,-11,updateregn);
  142.              moveto(prect.a.left+1,prect.a.bottom-2);
  143.              }
  144. /**************** Expand tabs by outputting spaces */
  145.                   if(tempbuf[i] == '\011')
  146.              {
  147.              drawchar(' ');
  148.              j++;
  149.              for(;j & 07;j++)
  150.                 drawchar(' ');
  151.              }
  152.           }
  153.            }
  154.         }
  155.      }
  156.  
  157. /**** Get the next event */
  158.  
  159.       systemtask();
  160.       temp = getnextevent(everyevent, &myevent);
  161.       switch (myevent.what)
  162.          {
  163.      case mousedown:  /* mouse down, call findwindow to figure out where */
  164.         code = findwindow(&myevent.where, &whichwindow);
  165.         switch (code)
  166.            {
  167.            case inmenubar:    /* in meun bar, execute the menu command */ 
  168.               docommand(menuselect(&myevent.where));
  169.           break;
  170.                case insyswindow:    /* in desk accessory, call desk manager */
  171.               systemclick(&myevent, whichwindow); 
  172.           break;
  173.            case indrag:    /* in drag, call dragwindow to move it */
  174.               dragwindow(whichwindow, &myevent.where, &dragrect);
  175.               break;
  176.            case incontent:    /* in content area, make application window the frontmost */
  177.            if (whichwindow != frontwindow())
  178.               selectwindow(whichwindow);
  179.            break;
  180.            }
  181.         break;
  182.      case keydown:    /* If keydown event, check for menu command key */
  183.         if(myevent.modifiers & cmdkey)
  184.            docommand(menukey((char)(myevent.message & 0377)));
  185.         break;
  186.      case autokey:
  187.         break;
  188.      case activateevt:    /* Application window becomming active, do nothing */
  189.         if((myevent.modifiers & 1)&&(((windowptr)myevent.message) == mywindow))
  190.            {
  191.            disableitem(mymenus[3],0);
  192.            enableitem(mymenus[2],0);
  193.            drawmenubar();
  194.            }
  195.         else
  196.            {
  197.            enableitem(mymenus[3],0);
  198.            disableitem(mymenus[2],0);
  199.            drawmenubar();
  200.            }
  201.         break;
  202.      case updateevt:    /* Update event, update the window frame */
  203.         if(((windowptr)myevent.message) == mywindow)
  204.            {
  205.            beginupdate(mywindow);
  206.            endupdate(mywindow);
  207.            }
  208.         break;
  209.     }
  210.     } while (doneflag == 0);
  211. }
  212.  
  213. /*---------------------------------------------------------------------------*/
  214. /* setupmenus()---This subroutine sets up the menu bar and reads in the desk
  215. ** accessory menu
  216. */
  217.  
  218. setupmenus()
  219. {
  220.    int i;
  221.  
  222. /* Apple menu, \024 is the apple character, adresmenu call loads all type DRVR resources */
  223.    mymenus[1] = newmenu(APPLEMENU, "\024");
  224.    appendmenu(mymenus[1], "About \"Display a File\";(-");
  225.    addresmenu(mymenus[1], "DRVR");
  226. /* File menu with open, close and quit selections */
  227.    mymenus[2] = newmenu(FILEMENU, "File");
  228.    appendmenu(mymenus[2], "Open/O;Close/C;Quit/Q");
  229. /* Edit menu with cut, copy and paste */
  230.    mymenus[3] = newmenu(EDITMENU, "Edit");
  231.    appendmenu(mymenus[3], "Undo;(-;Cut;Copy;Paste;Clear;(-;Show Clipboard");
  232. /* Stop scroll menu */
  233.    mymenus[4] = newmenu(STOPMENU,"Click Here to Pause Printout");
  234.    appendmenu(mymenus[4], "Release mouse button to resume printout");
  235.    for (i=1; i<=LASTMENU; i++)
  236.       insertmenu(mymenus[i], 0);
  237. /* Draw the completed menu bar */
  238.    drawmenubar();
  239. }
  240. /*---------------------------------------------------------------------------*/
  241. /* docommand(themenu, theitem)---this subroutine processes commands from the
  242. ** menu bar.  Themenu is the menu ID, theitem is the item number in the menu
  243. */
  244.  
  245. docommand(themenu, theitem)
  246. int themenu, theitem;
  247. {
  248.    char name[256];
  249.    point  openp;
  250.    sfreply rep;
  251.    sftypelist typelist;
  252.    int i;
  253.  
  254. /* Switch to decide what menu the cursor is in */
  255.    
  256.    switch (themenu)
  257.       {
  258.       case APPLEMENU:    /* Mouse down in apple menu */
  259. /******* Item one is the "about Display a file" box */
  260.          if(theitem == 1)
  261.         {
  262.         textfont(systemfont);
  263.         textsize(12);
  264.         eraserect(&prect);
  265.         moveto(prect.a.left,prect.a.top+70);
  266.         center("Display a file program");
  267.         center("Copyright 1985 by Greg Corson");
  268.         center("Kangaroo Koncepts, Inc.");
  269.         center("19141 Summers Drive");
  270.         center("South Bend, IN 46637");
  271.         center("(219) 277-5306");
  272.         textfont(monaco);
  273.             textsize(9); 
  274.         move(0,-3);
  275.         center("Feel free to give this program away to all your friends.");
  276.         center("It should NOT be sold for profit.  Be sure to try our");
  277.         center("Computer Based Communications System \"The Connection\"");
  278.         center("Free demo line (219) 277-5825 available 24 hours at 300 or");
  279.         center("1200 baud.  Be sure to look at the \"MacTech\" special");
  280.         center("interest group for information of interest to Mac");
  281.         center("programmers and the \"macintosh\" SIG for general info.");
  282.         pretty();
  283.         moveto(prect.a.left+1,prect.a.bottom-2);
  284.         }
  285. /******* The rest of the items are desk accessorys */
  286.          else
  287.         {
  288.         getitem(mymenus[1], theitem, name);
  289.             refnum = opendeskacc(name);
  290.         setport(mywindow);
  291.         }
  292.      break;
  293.       case FILEMENU:    /* Mouse down in file menu */
  294.          switch(theitem)
  295.         {
  296.         case 1:        /* Open file */
  297.            openp.a.v = 100;
  298.            openp.a.h = 60;
  299.            strncpy(&typelist[0],"TEXT",4);
  300.            sfgetfile(&openp.a,"",NULL,1, typelist, NULL, &rep);
  301.            if(rep.good)
  302.               {
  303.           if(fileopen)
  304.                  {
  305.              fsclose(fd1);
  306.              fileopen = FALSE;
  307.              }
  308.           if(fsopen(rep.fname,rep.vrefnum,&fd1) == noerr)
  309.              {
  310.              scrollrect(&prect,0,-11,updateregn);
  311.              fileopen=TRUE;
  312.              }
  313.           }
  314.            break;
  315.         case 2:        /* Close file */
  316.            if(fileopen)
  317.               {
  318.           fsclose(fd1);
  319.           fileopen = FALSE;
  320.           }
  321.            break;
  322.         case 3:        /* Quit */
  323.            if(fileopen)
  324.               {
  325.           fsclose(fd1);
  326.           fileopen = FALSE;
  327.           }
  328.            doneflag = 1;
  329.            break;
  330.         }
  331.      break;
  332.       case EDITMENU:    /* Process system edit events */
  333.          systemedit(theitem-1);
  334.      break;
  335.    }
  336.    hilitemenu(0);
  337. }
  338. /* Center a string in the window */
  339. center(str)
  340. char   *str;
  341. {
  342.    move(((wide-stringwidth(str))/2), 0);
  343.    drawstring(str);
  344.    move(-(mywindow->pnloc.a.h),(mywindow->txsize)+2);
  345. }
  346. /* draw a pretty design */
  347. pretty()
  348. {
  349.    int j;
  350.    rect tmprec;
  351.    
  352.    blockmove(&prect, &tmprec, (long)sizeof prect);
  353.    for(j=0;j < 12;j++)
  354.       {
  355.       frameoval(&tmprec);
  356.       insetrect(&tmprec,6,0);
  357.       }
  358.    blockmove(&prect, &tmprec, (long)sizeof prect);
  359.    for(j=0;j < 9;j++)
  360.       {
  361.       frameoval(&tmprec);
  362.       insetrect(&tmprec,0,6);
  363.       }
  364. }
  365.    
  366.                     
  367.